package cn.yikaojiuguo.student.ui.my;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.User;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import cn.yikaojiuguo.student.support.volley.Response;
import cn.yikaojiuguo.student.ui.base.BaseFragment;

/**
 * Created by cfan_yang on 2015/12/18.
 */
public class MyFragment extends BaseFragment implements View.OnClickListener{

    private View mRoot;
    //用户名
    @Bind(R.id.tv_name)
    TextView mTv_name;
    //年级
    @Bind(R.id.tv_grade)
    TextView mTv_grade;
    //头像
    @Bind(R.id.iv_avatar)
    ImageView mIv_avatar;
    //返回箭头
    @Bind(R.id.iv_back)
    ImageView mIv_back;
    //设置图标
    @Bind(R.id.iv_operation)
    ImageView mIv_operaton;
    //title
    @Bind(R.id.tv_title)
    TextView mTv_title;
    //金币
    @Bind(R.id.tv_coin)
    TextView mTv_coin;
    //头像等
    @Bind(R.id.rl_header)
    RelativeLayout mRl_header;
    //我的金币
    @Bind(R.id.rl_coin)
    RelativeLayout mRl_coin;
    //我的老师
    @Bind(R.id.rl_teacher)
    RelativeLayout mRl_teacher;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        initView();
        initEvents();
        return mRoot;
    }

    private void initEvents() {
        YiKaoApplication.changeUserInfoCache(true);
        mRl_header.setOnClickListener(this);
        mRl_coin.setOnClickListener(this);
        mRl_teacher.setOnClickListener(this);
        mIv_operaton.setOnClickListener(this);
    }

    private void initView() {
        mRoot = View.inflate(getContext(), R.layout.fragment_my,null);
        ButterKnife.bind(this, mRoot);
        mIv_back.setVisibility(View.GONE);
        mIv_operaton.setVisibility(View.VISIBLE);
        mIv_operaton.setImageResource(R.drawable.home_setting);
        mTv_title.setVisibility(View.VISIBLE);
        mTv_title.setText("我的");
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    /**
     * 如果本地有缓存，则从本地读取，如果没有，联网并缓存。
     */
    private void getData() {
        LogUtil.showInfoLog(mTag,"getData()");
        if(YiKaoApplication.isUserInfoCache()) {//有缓存
            showInfo();
        } else {//没有缓存
            showProgressDialogNotCancel();
            String requestUrl = UrlConstants.GET_USER_INFO + "&mid=" + YiKaoApplication.getUserId();
            RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    dismissProgressDialog();
                    if(response!=null && response.optBoolean("success")) {
                        Gson gson = new Gson();
                        User user = gson.fromJson(response.optJSONObject("data").toString(),User.class);
                        YiKaoApplication.putUserInfo(user);
                        YiKaoApplication.setAliasAndTags(YiKaoApplication.getUserId());
                        showInfo();
                    }
                }
            },null,mTag);
        }

    }

    private void showInfo() {
        if(YiKaoApplication.isUserInfoCacheChanged()) {
            ImageLoader.getInstance().displayImage(YiKaoApplication.getUserAvatar(), mIv_avatar);
            mTv_name.setText(YiKaoApplication.getUserName());
            mTv_grade.setText(YiKaoApplication.getGradeName());
            String goldStr = getResources().getString(R.string.fragment_my_my_coin) + " (" + YiKaoApplication.getGoldCoinNum() + ")";
            SpannableString ss = new SpannableString(goldStr);
            ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.text_gray)), 5,
                    YiKaoApplication.getGoldCoinNum().length() + 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTv_coin.setText(ss);
            YiKaoApplication.changeUserInfoCache(false);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //进入我的资料
            case R.id.rl_header:
                startActivity(MyInfoActivity.newIntent());
                break;
            //进入我的金币
            case R.id.rl_coin:
                startActivity(MyGoldActivity.newIntent());
                break;
            //进入我的老师
            case R.id.rl_teacher:
                startActivity(MyTeacherActivity.newIntent());
                break;
            //设置
            case R.id.iv_operation:
                startActivity(SettingActivity.newIntent());
                break;
        }
    }
}
